package com.agileboot.admin.controller.medicine;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.medicine.MedicineApplicationService;
import com.agileboot.domain.medicine.command.ChangeStatusMedicineCommand;
import com.agileboot.domain.medicine.command.MedicineCommand;
import com.agileboot.domain.medicine.dto.MedicineDTO;
import com.agileboot.domain.medicine.query.MedicineQuery;
import com.agileboot.domain.medicine.query.MedicineSync;
import com.agileboot.infrastructure.annotations.unrepeatable.Unrepeatable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.ArrayList;
import java.util.List;


/**
 * (Medicine)表控制层
 *
 * @author lx
 * @since 2023-12-02 14:20:00
 */
@Tag(name = "草药API", description = "草药增删查改")
@Validated
@RestController
@RequestMapping("/medicine")
@RequiredArgsConstructor
public class MedicineController extends BaseController {
    /**
     * 服务对象
     */
     private final MedicineApplicationService medicineApplicationService;
//     @Resource
//     private RabbitTemplate rabbitTemplate;

    @Operation(summary = "草药列表导入")
    @AccessLog(title = "草药导入管理", businessType = BusinessTypeEnum.IMPORT)
//    @PreAuthorize("@permission.has('system:user:import')")
    @PostMapping("/excel")
    public ResponseDTO<Void> importMedicineByExcel(MultipartFile file) {
        List<MedicineCommand> commands = CustomExcelUtil.readFromRequest(MedicineCommand.class, file);

        for (MedicineCommand command : commands) {
            medicineApplicationService.addMedicine(command);
        }
        return ResponseDTO.ok();
    }

    /**
     * 获取草药列表
     */
    @Operation(summary = "草药列表")
//    @PreAuthorize("@permission.has('system:user:list')")
    @GetMapping
    public ResponseDTO<PageDTO<MedicineDTO>> medicineList(MedicineQuery medicineQuery) {
        PageDTO<MedicineDTO> medicineDTOPage = medicineApplicationService.getMedicineList(medicineQuery);
        return ResponseDTO.ok(medicineDTOPage);
    }

    /**
     * 新增草药
     */
    @Operation(summary = "新增草药")
    @Unrepeatable(interval = 60, checkType = Unrepeatable.CheckType.SYSTEM_USER)
//    @PreAuthorize("@permission.has('system:user:list')")
    @AccessLog(title = "通知公告", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> medicineAdd(@RequestBody MedicineCommand medicineCommand) {
        medicineApplicationService.addMedicine(medicineCommand);
//        rabbitTemplate.convertAndSend(MqConstants.EXCHANGE_TOPIC, MqConstants.INSERT_KEY, medicineCommand.getName());
        return ResponseDTO.ok();
    }

    /**
     * 草药详情
     */
    @Operation(summary = "草药详情")
//    @PreAuthorize("@permission.has('system:user:list')")
    @GetMapping("/info")
    public ResponseDTO<MedicineDTO> getMedicineDetail(Long id) {
        MedicineDTO medicineDTO = medicineApplicationService.getMedicineDetail(id);
        return ResponseDTO.ok(medicineDTO);
    }

    @Operation(summary = "修改草药详情")
//    @PreAuthorize("@permission.has('system:user:list')")
    @PutMapping("/{id}")
    public ResponseDTO<Void> editMedicineDetail(@PathVariable Long id,@RequestBody MedicineDTO medicineDTO) throws Exception {
        medicineDTO.setId(id.toString());
        medicineApplicationService.editMedicineDetail(medicineDTO);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除草药")
//    @PreAuthorize("@permission.has('system:user:list')")
    @DeleteMapping("/{ids}")
    public ResponseDTO<Void> delMedicineDetail(@PathVariable List<Long> ids) {
        medicineApplicationService.delMedicineDetail(ids);
        return ResponseDTO.ok();
    }


    /**
     * 状态修改
     */
    @Operation(summary = "修改草药状态")
    @AccessLog(title = "草药管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{id}/status")
    public ResponseDTO<Void> changeStatus(@PathVariable Long id, @RequestBody ChangeStatusMedicineCommand command) {
        command.setId(id);
        medicineApplicationService.changeMedicineStatus(command);
        return ResponseDTO.ok();
    }

    /**
     * 同步草药数据到ES
     */
    @Operation(summary = "全量-》同步草药数据到ES")
    @GetMapping("/sync/all")
    public ResponseDTO<Void> syncMedicineToElasticSearch() throws Exception {
        medicineApplicationService.syncMedicineToElasticSearch();
        return ResponseDTO.ok();
    }

    /**
     * 同步草药数据到ES
     */
    @Operation(summary = "增量-》同步草药数据到ES")
    @PostMapping("/sync/batchId")
    public ResponseDTO<Void> syncMedicineToESByIds(@RequestBody MedicineSync medicineSync) throws Exception {
        if (StringUtils.isBlank(medicineSync.getIds())) {
            throw new ApiException(ErrorCode.Business.COMMON_BULK_DELETE_IDS_IS_INVALID);
        }
        String[] split = medicineSync.getIds().split(",");
        List<Long> list = new ArrayList<>();
        for (String str : split) {
            list.add(Long.valueOf(str));
        }
        medicineApplicationService.syncMedicineToESByIds(list);
        return ResponseDTO.ok();
    }


}

